package com.example.stringbufferlogger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stringbufferlogger.Utils.DataComparator;
import com.example.stringbufferlogger.VO.ComparisonResult;
import com.example.stringbufferlogger.VO.PageResponse;
import com.example.stringbufferlogger.dto.CompareDataDTO;
import com.example.stringbufferlogger.entity.LogRecord;
import com.example.stringbufferlogger.entity.ReplayLog;
import com.example.stringbufferlogger.entity.ReplayResult;
import com.example.stringbufferlogger.mapper.ReplayLogMapper;
import com.example.stringbufferlogger.mapper.ReplayResultMapper;
import com.example.stringbufferlogger.service.LogRecordService;
import com.example.stringbufferlogger.service.ReplayResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class ReplayResultServiceImpl extends ServiceImpl<ReplayResultMapper, ReplayResult> implements ReplayResultService {

    @Resource
    private ReplayLogMapper replayLogMapper;
    @Resource
    private LogRecordService logRecordService;
    @Resource
    private ReplayResultMapper replayResultMapper;

    /**
     * 数据对比
     *
     * @param compareDataDTO
     * @return
     */

    @Override
    public ResponseEntity<String> replayResult(CompareDataDTO compareDataDTO) {

        //  1. 获取回放记录 回放和映射表联表查
        List<ReplayLog> replayLogList = replayLogMapper.getReplayLogByRequestId();
        for (ReplayLog replayLog : replayLogList) {

            Long id = replayLog.getId();
            String requestId = replayLog.getRequestId();
            String responseBody = replayLog.getResponseBody();
            log.info("回放结果：" + responseBody);

            // 2. 获取日志记录
            PageResponse<LogRecord> logRecordPageResponse = logRecordService.recordPageResponse(requestId, compareDataDTO.getPageNum(), compareDataDTO.getPageSize());
            List<LogRecord> logRecordList = logRecordPageResponse.getList();
            Set<String> ignoredFields = new HashSet<>();

            // 3. 遍历日志记录
            for (LogRecord logRecord : logRecordList) {
                String responseBody1 = logRecord.getResponseBody();

                DataComparator.DataFormat format1 = DataComparator.detectFormat(responseBody);
                DataComparator.DataFormat format2 = DataComparator.detectFormat(responseBody1);
                ComparisonResult result = DataComparator.compareData(responseBody, responseBody1, format1, format2, ignoredFields);
                log.info("日志记录:{},回放结果记录{}", responseBody1, responseBody);
                log.info("对比结果：{}", result.getDifferencesStr());

                ReplayResult replayResult = new ReplayResult();
                replayResult.setRequestId(requestId);
                replayResult.setReplayId(id);
                replayResult.setOriginalValue(responseBody1);
                replayResult.setReplayValue(responseBody);
                replayResult.setDiffValue(result.getDifferencesStr());
                if (!result.isSuccess()) {
                    System.out.println("错误信息：" + result.getErrorMessage());
                } else if (!result.isDifferences()) {
                    log.info("两个JSON对象相等");
                    replayResult.setDiffFlag("0");
                } else {
                    log.info("存在差异:{}", result.getDifferencesStr());
                    replayResult.setDiffFlag("1");
                }
                replayResult.setReplayTime(LocalDateTime.now());
                log.info("日志记录" + responseBody1);

                // 存到流量回放结果记录表中
                replayResultMapper.insert(replayResult);
            }
        }
        return ResponseEntity.ok("保存记录表成功！");

    }

}
